﻿using Microsoft.Office.Core;
using System;
using System.Runtime.InteropServices;

namespace ExcelCOMAddin
{
    [System.Runtime.InteropServices.Guid("5FA4884F-AA70-40C0-A4EC-F5390930B152")]
    [System.Runtime.InteropServices.InterfaceType(
        System.Runtime.InteropServices.ComInterfaceType.InterfaceIsDual)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public interface IMyConnect : Extensibility.IDTExtensibility2, IRibbonExtensibility
    {
        [DispId(1)]
        string Greett(string name); 
        new void OnConnection(
            object Application,
            Extensibility.ext_ConnectMode ConnectMode,
            object AddInInst,
            ref Array custom);
        new void OnDisconnection(
            Extensibility.ext_DisconnectMode RemoveMode,
            ref Array custom);
        new void OnAddInsUpdate(ref Array custom);
        new void OnStartupComplete(ref Array custom);
        new void OnBeginShutdown(ref Array custom);

        new string GetCustomUI(string RibbonID);
    }
    [System.Runtime.InteropServices.ProgId("ExcelCOMAddin.MyConnect")]
    [System.Runtime.InteropServices.ComDefaultInterface(typeof(IMyConnect))]
    [System.Runtime.InteropServices.Guid("DE3911A1-C82F-42F1-BEFB-F3802E1B6D34")]
    [System.Runtime.InteropServices.ClassInterface(
        System.Runtime.InteropServices.ClassInterfaceType.None)]
    [System.Runtime.InteropServices.ComVisible(true)]
    public class MyConnect : IMyConnect
    {
        Microsoft.Office.Interop.Excel.Application _ApplicationObject;
        Microsoft.Office.Core.COMAddIn _AddinInstance;
        public string Greett(string name)
        {
            return $"Hello, {name}!";
        }

        public void OnConnection(
            object Application,
            Extensibility.ext_ConnectMode ConnectMode,
            object AddInInst,
            ref Array custom)
        {
            this._ApplicationObject = (Microsoft.Office.Interop.Excel.Application)Application;

            if (this._AddinInstance == null)
            {
                this._AddinInstance = (Microsoft.Office.Core.COMAddIn)AddInInst;
                this._AddinInstance.Object = this;
            }

            System.Windows.Forms.MessageBox.Show("OnConnection===1");
        }
        public void OnDisconnection(
            Extensibility.ext_DisconnectMode RemoveMode,
            ref Array custom)
        {
            this._ApplicationObject = null;
            this._AddinInstance = null;
            //System.Windows.Forms.MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().Name);

        }
        public void OnAddInsUpdate(ref Array custom)
        {
            //System.Windows.Forms.MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
        public void OnStartupComplete(ref Array custom)
        {
            //System.Windows.Forms.MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().Name);
        }
        public void OnBeginShutdown(ref Array custom)
        {
            //System.Windows.Forms.MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().Name);
        }

        public string GetCustomUI(string RibbonID)
        {
            //System.Windows.Forms.MessageBox.Show(System.Reflection.MethodBase.GetCurrentMethod().Name);

            return "";
        }
    }

    [ComVisible(true)]
    [Guid("12345678-1234-1234-1234-1234567890AB")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IMyFunctions
    {
        [DispId(1)]
        string Greet(string name);

        [DispId(2)]
        int Sum(int a, int b);
    }

    [ComVisible(true)]
    [Guid("87654321-4321-4321-4321-0987654321BA")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("MyCompany.MyAddIn")] // 设置ProgID
    public class MyFunctions : IMyFunctions
    {
        public string Greet(string name)
        {
            return $"Hello, {name}!";
        }

        public   int Sum(int a, int b)
        { 
            return   a + b;
        }
    }
}